package com.wuyi.tea.util;

import io.minio.*;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Component
public class MinioUtil {

    // minio服务器ip
    private String endpoint="http://192.168.31.208:5000";
    // 桶名
    private String bucketName="wuyi-tea";
    // access key
    private String accessKey="Fl3kKaDSUlGqxYFPjROi";
    // secret key
    private String secretKey="mlYNvVAAQjulW4QpTYdMgyJyfB7qk2tavwZm1VDR";


    /**
     * @param fileInputStream 要上传文件的输入流
     * @param minioFileName 推送到minio之后的名称
     */
    public String upload(InputStream fileInputStream, String minioFileName){
        // 1. 拿到minio客户端
        MinioClient minioClient= MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey,secretKey)
                .build();

        // 2. 判断bucket是否存在，若不存在，则创建bucket
        try {
            if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())){
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            }
        }catch (Exception e) {
            e.printStackTrace();
        };

        // 3. 上传文件
        try {

            minioClient.putObject(PutObjectArgs.builder()
                            .object(minioFileName)
                            .bucket(bucketName)
                            .stream(fileInputStream, fileInputStream.available(), -1)
                    .build());
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("上传失败");
            return null;
        }
        String url=endpoint+"/"+bucketName+"/"+minioFileName;
        return url;
    }

    /**
     * @param multipartFile 要上传的文件
     * @param minioFileName 要上传到minio的文件名
     * @return
     */
    public String upload(MultipartFile multipartFile, String minioFileName){
        String url=null;
        try {
            url = upload(multipartFile.getInputStream(),minioFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }

    /**
     * @param multipartFile 要上传的文件
     */
    public String upload(MultipartFile multipartFile){
        String url=null;
        try {
            String minioFileName = UUID.randomUUID() + multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
            url = upload(multipartFile.getInputStream(),minioFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }

    /**
     * @param fromMinioFileName 要下载的minio中的文件名
     * @param toLocalFileName   下载到本地的位置
     */
    public void download(String fromMinioFileName,String toLocalFileName){
        // 1.创建minio客户端
        MinioClient minioClient = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey,secretKey)
                .build();

        // 2.判断桶是否存在
        try {
            if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())){
                System.out.println(bucketName+" 这个桶不存在!");
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 3.下载文件
        try {
            minioClient.downloadObject(DownloadObjectArgs.builder()
                    .bucket(bucketName).object(fromMinioFileName).filename(toLocalFileName)
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(fromMinioFileName+" 下载失败!");
        }
    }
}
